package org.eiahe.hr.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.utils.MapstructUtils;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.attendance.domain.AnnualLeaveRule;
import org.eiahe.hr.attendance.domain.bo.AnnualLeaveRuleBo;
import org.eiahe.hr.attendance.domain.vo.AnnualLeaveRuleVo;
import org.eiahe.hr.attendance.mapper.AnnualLeaveRuleMapper;
import org.eiahe.hr.attendance.service.IAnnualLeaveRuleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 年假规则服务实现
 *
 * @author zhangnn
 */
@RequiredArgsConstructor
@Service
public class AnnualLeaveRuleServiceImpl implements IAnnualLeaveRuleService {

    private final AnnualLeaveRuleMapper baseMapper;

    @Override
    public AnnualLeaveRuleVo queryById(Long ruleId) {
        return baseMapper.selectVoById(ruleId);
    }

    @Override
    public List<AnnualLeaveRuleVo> queryList(AnnualLeaveRuleBo bo) {
        LambdaQueryWrapper<AnnualLeaveRule> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public TableDataInfo<AnnualLeaveRuleVo> queryPageList(AnnualLeaveRuleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AnnualLeaveRule> lqw = buildQueryWrapper(bo);
        Page<AnnualLeaveRuleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<AnnualLeaveRule> buildQueryWrapper(AnnualLeaveRuleBo bo) {
        LambdaQueryWrapper<AnnualLeaveRule> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getRuleName()), AnnualLeaveRule::getRuleName, bo.getRuleName());
        lqw.eq(StringUtils.isNotBlank(bo.getIsActive()), AnnualLeaveRule::getIsActive, bo.getIsActive());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), AnnualLeaveRule::getStatus, bo.getStatus());
        lqw.orderByAsc(AnnualLeaveRule::getMinWorkYears);
        return lqw;
    }

    @Override
    public Boolean insertByBo(AnnualLeaveRuleBo bo) {
        AnnualLeaveRule add = MapstructUtils.convert(bo, AnnualLeaveRule.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRuleId(add.getRuleId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(AnnualLeaveRuleBo bo) {
        AnnualLeaveRule update = MapstructUtils.convert(bo, AnnualLeaveRule.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    private void validEntityBeforeSave(AnnualLeaveRule entity) {
        // 校验工龄范围
        if (entity.getMinWorkYears() != null && entity.getMaxWorkYears() != null) {
            if (entity.getMinWorkYears() > entity.getMaxWorkYears()) {
                throw new RuntimeException("最小工龄不能大于最大工龄");
            }
        }
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 激活年假规则
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean activeRule(Long ruleId) {
        // 先将所有规则设为不生效
        LambdaUpdateWrapper<AnnualLeaveRule> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.set(AnnualLeaveRule::getIsActive, "0");
        baseMapper.update(null, updateWrapper);

        // 再将指定规则设为生效
        AnnualLeaveRule rule = new AnnualLeaveRule();
        rule.setRuleId(ruleId);
        rule.setIsActive("1");
        return baseMapper.updateById(rule) > 0;
    }

    /**
     * 查询当前生效的年假规则
     */
    @Override
    public AnnualLeaveRuleVo queryActiveRule() {
        LambdaQueryWrapper<AnnualLeaveRule> lqw = Wrappers.lambdaQuery();
        lqw.eq(AnnualLeaveRule::getIsActive, "1");
        lqw.eq(AnnualLeaveRule::getStatus, "0");
        lqw.last("LIMIT 1");
        return baseMapper.selectVoOne(lqw);
    }

    @Override
    public AnnualLeaveRuleVo getInfoByDay(Integer days) {
        if (days == null) {
            return null;
        }
        Integer workYears = days.intValue();

        LambdaQueryWrapper<AnnualLeaveRule> lqw = Wrappers.lambdaQuery();
        lqw.eq(AnnualLeaveRule::getIsActive, "1");
        lqw.eq(AnnualLeaveRule::getStatus, "0");
        lqw.le(AnnualLeaveRule::getMinWorkYears, workYears);
        lqw.ge(AnnualLeaveRule::getMaxWorkYears, workYears);
        lqw.last("LIMIT 1");
        return baseMapper.selectVoOne(lqw);
    }
}
